package com.topisv.tms.web.stock.check;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.stock.check.CheckPlanSetting;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.stock.check.CheckPlanSettingService;
import com.topisv.tms.utils.enums.StatusEnums;

/**
 * 盘点周期设置
 *  Date          Author       Version       Description
 * ----------------------------------------------------------
 *  2014-7-15      CC            1.0           Create
 */
@Controller
@RequestMapping("/v2/checkPlanSetting")
public class CheckPlanSettingContorller {
	@Autowired
	private CustomersService customersService;
	@Autowired
	private AreaGroupService areaGroupService;
	@Autowired
	private CheckPlanSettingService checkPlanSettingService;
	
	/**
	 * 导向盘点周期设置页面
	 * @param model
	 * @param request
	 * @return String
	 */
	@RequestMapping("/toPage")
	public String toPage(Model model) {
		fillSelectValue(model, null);
		return "stock/check/checkPlanSetting";
	}
	
	/**
	 * 查找数据
	 * @param model
	 * @param customerCode
	 * @return String
	 */
	@RequestMapping("/queryCheckPlanSetting")
	public String queryCheckPlanSetting(Model model,
			@RequestParam("customerCode") String customerCode){
		fillSelectValue(model, customerCode);
		bulidCheckPlanSetting(model, customerCode,checkPlanSettingService.loadByCustomerCode(customerCode));
		return "stock/check/checkPlanSetting";
	}
	/**
	 * 保存
	 * @param plan
	 * @return String
	 */
	@RequestMapping("/saveCheckPlanSetting")
	@ResponseBody
	public CheckPlanSetting saveCheckPlanSetting(CheckPlanSetting plan){
		plan.setCreaterUuid(getCurUser().jobuuid);
		plan.setCreateTime(new Date());
		checkPlanSettingService.saveCheckPlanSetting(plan);
		return plan;
	}
	/**
	 * 构建盘点周期
	 * @param model
	 * @param customerCode
	 * @param checkSetting
	 */
	@SuppressWarnings("unchecked")
	private void bulidCheckPlanSetting(Model model,String customerCode,List<CheckPlanSetting> checkSetting){
		CheckPlanSetting setting = null;
		List<AreaGroup> areaGroups = (List<AreaGroup>)(model.asMap().get("warehouseList"));
		for (AreaGroup areaGroup : areaGroups) {
			if (!exists(checkSetting, customerCode, areaGroup.getGroupCode())) {
				setting = new CheckPlanSetting();
				setting.setCustomerCode(customerCode);
				setting.setWarehouseCode(areaGroup.getGroupCode());
				checkSetting.add(setting);
			}
		}
		model.addAttribute("data", checkSetting);
	}
	
	/**
	 * 校验是否存在盘点设置
	 * @param checkSetting
	 * @param customerCode
	 * @param groupCode
	 * @return boolean
	 */
	private boolean exists(List<CheckPlanSetting> checkSetting, String customerCode, String groupCode) {
		for (CheckPlanSetting checkPlanSetting : checkSetting) {
			if (checkPlanSetting.getCustomerCode().equals(customerCode) && checkPlanSetting.getWarehouseCode().equals(groupCode)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 根据用户权限查找商户组以及仓库
	 * @param model
	 * @param customerCode
	 */
	private void fillSelectValue(Model model, String customerCode) {
		// 查找商户组
		ShiroUser curUser = getCurUser();
		List<Customers> customerList = null;
		if (StatusEnums.user_type_paiu.getStatus().equals(curUser.userType)) { // 派友用户
			customerList = customersService.findAll();
		} else if (StatusEnums.user_type_customer.getStatus().equals(curUser.userType)) { // 商家用户
			customerList = new ArrayList<Customers>();
			Customers customer = customersService.findByCustomerCode(curUser.customerCode);
			customerList.add(customer);
		}
		model.addAttribute("customerList", customerList);
		List<AreaGroup> warehouseList = null;
		if (StringUtils.isNotBlank(customerCode)) {
			warehouseList = this.areaGroupService.loadAreaGroupByAuthority(curUser, customerCode);
		} else {
			if (StatusEnums.user_type_customer.getStatus().equals(curUser.userType)) {
				warehouseList = areaGroupService.loadAreaGroupByAuthority(curUser, curUser.customerCode);
			} else if (StatusEnums.user_type_paiu.getStatus().equals(curUser.userType)) {
				warehouseList = areaGroupService.findAll();
			}
		}
		model.addAttribute("warehouseList", warehouseList);
	}
	private ShiroUser getCurUser(){
		return (ShiroUser) SecurityUtils.getSubject().getPrincipal();
	}
}
